package net.awpspace.mobile.management.note;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.form.BaseForm;

import com.sun.lwuit.Button;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;

public class ViewDetailNote extends BaseForm{

	private Note note;
	private int index;
	private Button edit,delete;
	private AudioManager audio = AudioManager.getInstance();
	
	public ViewDetailNote(MIDlet app, BaseForm parent, Note note, int index) {
		super(app, parent);
		this.note = note;
		this.index = index;
		setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		createUI();
	}
	
	private void createUI(){
		Container wapper = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		
		wapper.addComponent(new Label("Ghi chú: "));
		TextField name = new TextField(note.getName());
		name.setEditable(false);
		wapper.addComponent(name);
		System.out.println("name: "+note.getName());
		
		wapper.addComponent(new Label("Nội dung: "));
		TextField content = new TextField(note.getContent());
		content.setEditable(false);
		wapper.addComponent(content);
		
		wapper.addComponent(new Label("Địa điểm: "));
		TextField place = new TextField(note.getPlace());
		place.setEditable(false);
		wapper.addComponent(place);
		
		int ngay = note.getNgay();
		int thang = note.getThang();
		int nam = note.getNam();
		
		wapper.addComponent(new Label("Thời gian:"));
		TextField time = new TextField(ngay+"/"+thang+"/"+nam);
		time.setEditable(false);
		wapper.addComponent(time);
		
		if(note.isAlarm()){
			wapper.addComponent(new Label("Âm báo lúc:"));
			int gio = note.getGio();
			int phut = note.getPhut();
			TextField alarm = new TextField(gio+":"+phut);
			alarm.setEditable(false);
			wapper.addComponent(alarm);
			
			wapper.addComponent(new Label("Lặp lại:"));
			if(note.isCn()){
				final CheckBox cn = new CheckBox("Chủ nhật");
				cn.setSelected(true);
				cn.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						cn.setSelected(true);
					}
				});
				wapper.addComponent(cn);
			}
			if(note.isT2()){
				final CheckBox day = new CheckBox("Thứ hai");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			if(note.isT3()){
				final CheckBox day = new CheckBox("Thứ ba");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			if(note.isT4()){
				final CheckBox day = new CheckBox("Thứ tư");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			if(note.isT5()){
				final CheckBox day = new CheckBox("Thứ năm");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			if(note.isT6()){
				final CheckBox day = new CheckBox("Thứ sáu");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			if(note.isT7()){
				final CheckBox day = new CheckBox("Thứ bảy");
				day.setSelected(true);
				day.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						day.setSelected(true);
					}
				});
				wapper.addComponent(day);
			}
			
		}
		
		edit = new Button("Sửa ghi chú");
		delete = new Button("Xóa");
		Container button_container = new Container(new GridLayout(1, 2));
		button_container.addComponent(edit);
		button_container.addComponent(delete);
		edit.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				audio.playSample("/button.mp3");
				new EditNote(app, ViewDetailNote.this, note, index).show();
			}
		});
		delete.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				audio.playSample("/button.mp3");
				Dao.delete(index);
				new ViewNotes(app, NoteManagement.getInstance()).show();
			}
		});
		
		wapper.addComponent(button_container);
		
		this.addComponent(wapper);
	}

	protected void processCommand(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}
